﻿
using Game2DFramework.Collision;
using Game2DFramework.Extensions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace MechGame.Game.Leveling
{
    class Item
    {
        const float AplitudePixels = 5.0f;
        private const float AmplyfySpeed = 4.0f;

        private readonly Texture2D _texture;
        private readonly Vector2 _position;
        private float _currentDelta;
        private float _currentDir;
        private float _currentVerticalOffset;

        public AttachedItem ItemType { get; private set; }

        public Circle Bounds {get { return new Circle((int) _position.X, (int) _position.Y, 12); }}

        public Item(Texture2D texture, Vector2 position, AttachedItem itemType)
        {
            _texture = texture;
            _position = position;
            _currentDir = 1;
            ItemType = itemType;
        }

        public void Update(float elapsed)
        {
            _currentDelta += _currentDir * elapsed * AmplyfySpeed;
            const float amplitude = 1.0f;
            if (_currentDir > 0 && _currentDelta >= amplitude || _currentDir < 0 && _currentDelta <= -amplitude)
            {
                _currentDelta = _currentDir * 2 * amplitude - _currentDelta;
                _currentDir = -_currentDir;
            }
            _currentVerticalOffset = _currentDelta * AplitudePixels;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, _position + new Vector2(0, _currentVerticalOffset), null, Color.White, 0.0f, _texture.GetCenter(), 1.0f, SpriteEffects.None, 0);
        }
    }
}
